//
//  AddRejectViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/11/24.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "AddRejectViewController.h"
#import "ComMethod.h"
#import "TSHeader.h"
#import "SocketRequest.h"
#import "CustomAlertView.h"
#import "Singleton.h"

#define TextViewLength  100

@interface AddRejectViewController ()<CustomAlertViewDelegate,SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation AddRejectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"添加拒收用户";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self rightBarButtonItemTitle:@"保存" Method:@selector(addRejectUser)];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(12, 40, 308, 0.5)];
    line.backgroundColor = LineColor;
    [_inputView addSubview:line];
    
    [_phoneTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)backLastViewController {
    [self.view endEditing:YES];
    if (_phoneTextField.text.length > 0 || _remarkTextView.text.length > 0) {
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:nil message:@"您的信息还未保存，是否确定返回？" delegate:self cancelButtonTitle:@"取消" sureButtonTitle:@"确定"];
        [alert show];
    } else {
        [super backLastViewController];
    }
}

- (void)autoBackLastViewController {
    if ([_delegate respondsToSelector:@selector(addRejectUserSuccess)]) {
        [_delegate addRejectUserSuccess];
    }
    [super backLastViewController];
}

- (void)addRejectUser {
    [self.view endEditing:YES];
    if (_phoneTextField.text.length <= 0) {
        [DSToast showText:@"请输入手机号" InView:self.view];
    } else if (![ComMethod judgeIsLegal:_phoneTextField.text Limit:TelephoneLimit]) {
        [DSToast showText:@"请输入正确的11位手机号" InView:self.view];
    } else {
        [ToastActivity makeActivity:@"添加中..." InView:self.view];
        [self addRejectUserSocket];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)addRejectUserSocket {
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STC1"]];
    [mutableData appendBytes:[@"1" UTF8String] length:1];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [mutableData appendBytes:[SocketMethod checkLegal:_phoneTextField.text] length:16];
    NSData *remarkData = [_remarkTextView.text dataUsingEncoding:NSUTF8StringEncoding];
    NSData *remarkLengthData = [SocketMethod hexDataWithLength:remarkData.length];
    [mutableData appendData:remarkLengthData];
    [mutableData appendData:remarkData];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        [ToastActivity makeSuccess:@"添加成功" InView:self.view];
        [self performSelector:@selector(autoBackLastViewController) withObject:nil afterDelay:0.5];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark CustomAlertViewDelegate
- (void)customAlertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [super backLastViewController];
    }
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:TelephoneLimit] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    if (![string isEqualToString:filtered]) {
        return NO;
    }
    return YES;
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    if (textField.text.length > 11) {
        textField.text = [textField.text substringToIndex:11];
    }
}

#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length <= 0) {
        _tipLabel.hidden = NO;
    } else {
        _tipLabel.hidden = YES;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        [self textViewText:textView.text];
    }
}

- (void)textViewText:(NSString *)text {
    if (text.length > TextViewLength) {
        _remarkTextView.text  = [text substringToIndex:TextViewLength];
        [_remarkTextView resignFirstResponder];
    }
    _numberLabel.text = [NSString stringWithFormat:@"%d/%d",(int)_remarkTextView.text.length,TextViewLength];
}
@end
